package com.dnzx.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.dnzx.framework.page.PageResult;
import org.springframework.stereotype.Service;

import com.dnzx.dao.IOrderItemDao;
import com.dnzx.model.OrderItemBO;
import com.dnzx.service.IOrderItemService;
import com.dnzx.service.ISubCategoryService;

/**
 * 订单
 */
@Service
public class OrderItemServiceImpl implements IOrderItemService {

    @Resource
    private IOrderItemDao orderItemDao;
    
    @Resource
    private ISubCategoryService subCategoryService;

    @Override
    public void save(OrderItemBO orderItemBO) throws Exception {
    	// 处理设备收款方式
    	List<String> paymentTypeDeviceList = orderItemBO.getPaymentTypeDeviceList();
    	if (paymentTypeDeviceList != null && paymentTypeDeviceList.size() > 0) {
    		String paymentTypeDevice = "";
    		for (String paymentTypeDeviceTemp : paymentTypeDeviceList) {
    			paymentTypeDevice = paymentTypeDevice + "," + paymentTypeDeviceTemp;
    		}
    		paymentTypeDevice = paymentTypeDevice.substring(1, paymentTypeDevice.length());
    		orderItemBO.setPaymentTypeDevice(paymentTypeDevice);
    	}
    	
    	// 处理副柜编号和数量
    	List<String> subCategoryCodeList = orderItemBO.getSubCategoryCodeList();
    	List<String> subCategoryCountList = orderItemBO.getSubCategoryCountList();
    	if (subCategoryCodeList != null && subCategoryCodeList.size() > 0) {
    		String subCategoryCode = "";
    		String subCategoryCount = "";
    		for (int i = 0; i < subCategoryCodeList.size(); i++) {
    			String subCategoryCodeTemp = subCategoryCodeList.get(i);
    			subCategoryCode = subCategoryCode + "," + subCategoryCodeTemp;
    			
    			String subCategoryCountTemp = subCategoryCountList.get(i);
    			subCategoryCount = subCategoryCount + "," + subCategoryCountTemp;
    		}
    		
    		subCategoryCode = subCategoryCode.substring(1, subCategoryCode.length());
    		subCategoryCount = subCategoryCount.substring(1, subCategoryCount.length());
    		orderItemBO.setSubCategoryCount(subCategoryCount);
    		orderItemBO.setSubCategoryCode(subCategoryCode);
    	}
    	
        this.orderItemDao.save(orderItemBO);
    }

    @Override
    public void delete(Long id) throws Exception {
        this.orderItemDao.delete(id);
    }

    @Override
    public void update(OrderItemBO orderItemBO) throws Exception {
        this.orderItemDao.update(orderItemBO);
    }

    @Override
    public OrderItemBO get(Long id) {
        return this.orderItemDao.get(id);
    }
    
    @Override
    public OrderItemBO get(OrderItemBO orderItemBO) {
    	List<OrderItemBO> orderItemBOList = this.orderItemDao.queryList(orderItemBO);
    	if (orderItemBOList != null && orderItemBOList.size() > 0) {
    		return orderItemBOList.get(0);
    	}
        return null;
    }
    
    @Override
    public List<OrderItemBO> queryList(OrderItemBO orderItemBO) {
        return this.orderItemDao.queryList(orderItemBO);
    }
    
    @Override
    public PageResult<List<OrderItemBO>> queryPage(OrderItemBO orderItemBO, PageResult<List<OrderItemBO>> pageResult) {
        int totalRows = this.orderItemDao.queryCount(orderItemBO);
        pageResult.setTotalRows(totalRows);
        pageResult.setCurrentPage(orderItemBO.getCurrentPage());
        pageResult.setPageSize(orderItemBO.getPageSize());
		List<OrderItemBO> records = this.orderItemDao.queryPage(pageResult.getPageSize(), pageResult.getStartRow(), orderItemBO);
		if (records != null && records.size() > 0) {
			for (OrderItemBO orderItemBOTemp : records) {
				String subCategoryCode = orderItemBOTemp.getSubCategoryCode();
				String subCategoryCount = orderItemBOTemp.getSubCategoryCount();
				if (subCategoryCode != null && !"".equals(subCategoryCode)) {
					String[] subCategoryCodes = subCategoryCode.split(",");
					String[] subCategoryCounts = subCategoryCount.split(",");
					List<String> subCategoryCodeList = new ArrayList<String>();
					List<String> subCategoryCountList = new ArrayList<String>();
					for (int i = 0; i < subCategoryCodes.length; i++) {
						subCategoryCodeList.add(subCategoryCodes[i]);
						subCategoryCountList.add(subCategoryCounts[i]);
					}
					orderItemBOTemp.setSubCategoryCodeList(subCategoryCodeList);
					orderItemBOTemp.setSubCategoryCountList(subCategoryCountList);
				}
			}
		}
		pageResult.setT(records);
		return pageResult;
    }

}
